package com.wuye.basic.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wuye.basic.domain.WyBalconyunithome;
import com.wuye.basic.domain.dto.WyBalconyDTO;
import com.wuye.common.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.wuye.basic.mapper.WyBalconyMapper;
import com.wuye.basic.domain.WyBalcony;
import com.wuye.basic.service.IWyBalconyService;

/**
 * 楼座Service业务层处理
 * 
 * @author avalon
 * @date 2024-11-25
 */
@Service
@RequiredArgsConstructor
public class WyBalconyServiceImpl extends ServiceImpl<WyBalconyMapper,WyBalcony>
        implements IWyBalconyService
{

    private final WyBalconyMapper wyBalconyMapper;
    private final WyBalconyunithomeServiceImpl wyBalconyunithomeService;


    @Override
    public List<WyBalcony> selectWyBalconyList(WyBalcony wyBalcony)
    {
        return wyBalconyMapper.selectWyBalconyList(wyBalcony);
    }


    @Override
    public List<WyBalcony> selectList(WyBalcony wyBalcony) {
        return lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(wyBalcony.getBalconyName()),WyBalcony::getBalconyName,wyBalcony.getBalconyName())
                .eq(wyBalcony.getCommunityId() != null,WyBalcony::getCommunityId,wyBalcony.getCommunityId())
                .list();
    }

    @Override
    public boolean insertBalcony(WyBalconyDTO wyBalconyDTO) {
        WyBalcony wyBalcony = BeanUtil.copyProperties(wyBalconyDTO, WyBalcony.class);
        wyBalcony.setCreateTime(DateUtils.getNowDate());
        wyBalcony.setUnitNumber((long) wyBalconyDTO.getUnitIds().size());
        this.save(wyBalcony);
        List<WyBalconyunithome> wyBalconyunithomes = new ArrayList<>();
        wyBalconyDTO.getUnitIds().forEach(
                item -> wyBalconyunithomes.add(
                        WyBalconyunithome.builder()
                                .balconyid(wyBalcony.getBalconyId())
                                .unitid(item)
                                .build()
                )
        );
        return wyBalconyunithomeService.saveBatch(wyBalconyunithomes);
    }

    @Override
    public WyBalconyDTO selectBybalconyId(Long balconyId) {
        WyBalconyDTO wyBalconyDTO = BeanUtil.copyProperties(this.getById(balconyId), WyBalconyDTO.class);
        List<WyBalconyunithome> list = Db.lambdaQuery(WyBalconyunithome.class)
                .eq(WyBalconyunithome::getBalconyid, balconyId)
                .isNull(WyBalconyunithome::getHomeid)
                .list();
        wyBalconyDTO.setUnitIds(
                list.stream().map(WyBalconyunithome::getUnitid).collect(Collectors.toList())
        );
        return wyBalconyDTO;
    }

    @Override
    public boolean removeByIdsAndDTO(List<Long> list) {
        this.removeByIds(list);
        return wyBalconyunithomeService.removeByIds(
                Db.lambdaQuery(WyBalconyunithome.class)
                        .in(WyBalconyunithome::getBalconyid, list)
                        .list().stream().map(WyBalconyunithome::getId).collect(Collectors.toList())
        );
    }

    @Override
    public boolean updateInfo(WyBalconyDTO wyBalconyDTO) {
        WyBalcony byId = this.getById(wyBalconyDTO.getBalconyId());
        List<Long> oldUnitIds = Db.lambdaQuery(WyBalconyunithome.class)
                .eq(WyBalconyunithome::getBalconyid, byId.getBalconyId())
                .isNull(WyBalconyunithome::getHomeid)
                .list().stream().map(WyBalconyunithome::getUnitid).collect(Collectors.toList());
        List<Long> newUnitIds = wyBalconyDTO.getUnitIds();
        List<Long> addUnitIds = newUnitIds.stream()
                .filter(newUnitId -> !oldUnitIds.contains(newUnitId))
                .collect(Collectors.toList());
        List<Long> delUnitIds = oldUnitIds.stream()
                .filter(oldUnitId -> !newUnitIds.contains(oldUnitId))
                .collect(Collectors.toList());
        List<WyBalconyunithome> wyBalconyunithomeList = addUnitIds.stream()
                .map(unitId -> WyBalconyunithome.builder()
                        .balconyid(byId.getBalconyId())
                        .unitid(unitId)
                        .build())
                .collect(Collectors.toList());
        if (!wyBalconyunithomeList.isEmpty()){
            wyBalconyunithomeService.saveBatch(wyBalconyunithomeList);
        }
        if(!delUnitIds.isEmpty()){
            wyBalconyunithomeService.removeByIds(
                    Db.lambdaQuery(WyBalconyunithome.class)
                            .eq(WyBalconyunithome::getBalconyid, byId.getBalconyId())
                            .in(WyBalconyunithome::getUnitid, delUnitIds)
                            .list().stream().map(WyBalconyunithome::getId).collect(Collectors.toList())
            );
        }
        WyBalcony wyBalcony = BeanUtil.copyProperties(wyBalconyDTO, WyBalcony.class);
        List<Long> unitIds = Db.lambdaQuery(WyBalconyunithome.class)
                .eq(WyBalconyunithome::getBalconyid, byId.getBalconyId())
                .isNull(WyBalconyunithome::getHomeid)
                .list().stream().map(WyBalconyunithome::getUnitid).collect(Collectors.toList());
        wyBalcony.setUnitNumber((long) unitIds.size());
        return this.updateById(wyBalcony);
    }
}
